<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<script>
	$(function(){ 
		$("#domainName").attr('enabled', 'false');
		var uri = "${pageContext.request.requestURI}";
		var tmps = uri.split("/");
		//var page = uri.substring(i+1, len);
		//i = page.lastIndexOf(".");
		var page = tmps[4];
		$("#"+page).attr("class", "active");
		
		if((page === 'summarypid' || page === 'summaryrep') && '${USER}' === ''){
			window.location = '${pageContext.request.contextPath}';
		}
		
		var loggedinByPidPage = false;
		var loggedinByREPPage = false;
		
		if("${USER}" === ''){
			$(".user").css("display","none");
		}else{
			$(".user").css("display","block");
		}
		
		$("#btnLogout").click(function(){
			$("#alertMsg").text("ต้องการออกจากระบบ?");
			$("#alertDlg").dialog({
				height: 160,
				width: 300,
				modal: true,
				buttons: {
					Ok: function() {
						window.location = "${pageContext.request.contextPath}/login/logout";
						$(this).dialog("close");
					},
					Cancel: function(){
						$(this).dialog("close");
					}
				}
			});
		});
		
		$.alert = function(code, msg){
			if(msg === undefined){
				msg = "";
			}
			$("#alertMsg").html("<br>" + msg);
			$("#alertDlg").dialog({
				height: 170,
				width: 300,
				modal: true,
				buttons: {
					Ok: function() {
						$(this).dialog("close");
					}
				}
			});
		};
		
		$(".nav ul li").hover(function(){
			$(this).find("ul").show("slideDown");
		},function(){
			$(this).find("ul").hide("slideUp");
		});
		
		
		$(".nav li a, .nav ul li strong").hover(function(){
			$(this).addClass("hover");
			$("#"+page).removeClass("active");
			$("#"+page).addClass("active");
			},function(){
			$(this).removeClass("hover");
			$("#"+page).addClass("active");
		});
		
		var msg = "โปรดเลือกเขตให้ตรงกับเขตของท่าน";
		
		$.loadpage = function(zoneId, pidPage, repPage){
			//var domainName = "${USER.domainName}";
			if(zoneId !== undefined && zoneId !== null){
				ZONE_ID = zoneId;
			}else{
				ZONE_ID = "01";
			}
			
			if("${USER}" === ""){
				loggedinByPidPage = pidPage;
				loggedinByREPPage = pidPage;
				$("#dlgLogin").dialog({
					modal: true,
					width: 550,
					height: 290
				});
				var domainName = "";
				$("#domainName").find('option').remove();
				$("#domainName").append('<option value="">-- กรุณาเลือก --</option>');
				$("#domainName").append('<option value="nhso.go.th">nhso.go.th</option>');
				switch (ZONE_ID) {
				case "01":
					domainName = "cmi.nhso.go.th";
					$("#domainName").append('<option value="'+domainName+'">'+domainName+'</option>');
					if(zoneId !== undefined && zoneId !== null){
						break;
					}
				case "02":
					domainName = "plk.nhso.go.th";
					$("#domainName").append('<option value="'+domainName+'">'+domainName+'</option>');
					if(zoneId !== undefined && zoneId !== null){
						break;
					}
				case "03":
					domainName = "nkw.nhso.go.th";
					$("#domainName").append('<option value="'+domainName+'">'+domainName+'</option>');
					if(zoneId !== undefined && zoneId !== null){
						break;
					}
				case "04":
					domainName = "srr.nhso.go.th";
					$("#domainName").append('<option value="'+domainName+'">'+domainName+'</option>');
					if(zoneId !== undefined && zoneId !== null){
						break;
					}
				case "05":
					domainName = "rbr.nhso.go.th";
					$("#domainName").append('<option value="'+domainName+'">'+domainName+'</option>');
					if(zoneId !== undefined && zoneId !== null){
						break;
					}
				case "06":
					domainName = "ryg.nhso.go.th";
					$("#domainName").append('<option value="'+domainName+'">'+domainName+'</option>');
					if(zoneId !== undefined && zoneId !== null){
						break;
					}
				case "07":
					domainName = "kkn.nhso.go.th";
					$("#domainName").append('<option value="'+domainName+'">'+domainName+'</option>');
					if(zoneId !== undefined && zoneId !== null){
						break;
					}
				case "08":
					domainName = "udt.nhso.go.th";
					$("#domainName").append('<option value="'+domainName+'">'+domainName+'</option>');
					if(zoneId !== undefined && zoneId !== null){
						break;
					}
				case "09":
					domainName = "nkm.nhso.go.th";
					$("#domainName").append('<option value="'+domainName+'">'+domainName+'</option>');
					if(zoneId !== undefined && zoneId !== null){
						break;
					}
				case "10":
					domainName = "ubn.nhso.go.th";
					$("#domainName").append('<option value="'+domainName+'">'+domainName+'</option>');
					if(zoneId !== undefined && zoneId !== null){
						break;
					}
				case "11":
					domainName = "srn.nhso.go.th";
					$("#domainName").append('<option value="'+domainName+'">'+domainName+'</option>');
					if(zoneId !== undefined && zoneId !== null){
						break;
					}
				case "12":
					domainName = "skl.nhso.go.th";
					$("#domainName").append('<option value="'+domainName+'">'+domainName+'</option>');
					if(zoneId !== undefined && zoneId !== null){
						break;
					}
				case "13":
					domainName = "bk.nhso.go.th";
					$("#domainName").append('<option value="'+domainName+'">'+domainName+'</option>');
					if(zoneId !== undefined && zoneId !== null){
						break;
					}
				}
			}else{
				if(pidPage === true){
		        	window.location = "${pageContext.request.contextPath}/summarypid";
		        }else if(repPage){
		        	window.location.href = '${pageContext.request.contextPath}/summaryrep';
		        }else if("${USER.userType}" === "Hospital"){
					$.ajax({
			            url : '${pageContext.request.contextPath}/master/getZoneByHospitalCode',
			            type: "POST",
			            data: {hospitalCode: "${USER.officeCode}"},
			            success : function(data) {
							if(data.success){
								if(data.code === "0000"){
									if(ZONE_ID === data.datas.zoneId){
										window.location = "${pageContext.request.contextPath}/summary/details?zoneId="+ZONE_ID+"&c=y";
									}else{
										$.alert("", msg);
									}
								}else{
								}
							}else{
			            		$.alert(data.code, data.msg);
							}
			            }
			        });
		        }else if("${USER.userType}" === "Employee"){
						$.ajax({
				            url : '${pageContext.request.contextPath}/master/getZoneFromDomainName',
				            type: "POST",
				            data: {domainName: "${USER.domainName}"},
				            success : function(data) {
								if(data.success){
									if(data.code === "0000"){
										if(ZONE_ID === data.datas.zoneId  || "00" === data.datas.zoneId) {
											window.location = "${pageContext.request.contextPath}/summary/details?zoneId="+ZONE_ID+"&c=y";
										}else{
				            				$.alert("", msg);
										}
									}
								}else{
				            		$.alert(data.code, data.msg);
								}
				            }
				        });
		        }
			}
			
			
			if(pidPage === true){
				return  true;
			}else if(repPage){
				return true;
			}else{
				return false;
			}
		};
		
		$("#txtUsername, #txtPassword").keypress(function(e){
			if(e.which === 13){
				$.Login(false, loggedinByPidPage, loggedinByREPPage);
			}
		});
		
		$("#summarypid").click(function(){
			loggedinByPidPage = $.loadpage(null, true, false);
			loggedinByREPPage = false;
		});
		$("#summaryrep").click(function(){
			loggedinByREPPage = $.loadpage(null, false, true);
			loggedinByPidPage = false;
		});
		
		$.Login = function(login, pidPage, repPage){
			var userType = $("input[name='userType']:checked").val();
			var domainName = $("#domainName").val();
			var username = $("#txtUsername").val();
			var password = $("#txtPassword").val();
			if(userType === "E"){
				if(domainName === ""){
					$("#domainName").focus();
					$.alert("", "โปรดระบุโดเมน");
					return false;
				}
			}
			if(username === ""){
				$("#txtUsername").focus();
				$.alert("", "โปรดระบุชื่อผู้ใช้งาน");
				return false;
			}
			
			if(password === ""){
				$("#txtPassword").focus();
				$.alert("", "โปรดระบุรหัสผ่าน");
				return false;
			}
			var data = {username: username, password: password, domainName: domainName,userType:userType};
			
			$.ajax({
	            url : '${pageContext.request.contextPath}/login/checkuser',
	            type: "POST",
	            data: data,
	            async: false,
	            success : function(data) {
					if(data.success){
						if(data.code === "0000"){
							if(loggedinByPidPage){
								window.location.href = "${pageContext.request.contextPath}/summarypid";
							}else if(loggedinByREPPage){
								window.location.href = "${pageContext.request.contextPath}/summaryrep";
							}else{
								if(userType === "H"){
									$.ajax({
							            url : '${pageContext.request.contextPath}/master/getZoneByHospitalCode',
							            type: "POST",
							            data: {hospitalCode: data.datas.officeCode},
	           	 						async: false,
							            success : function(data) {
											if(data.success){
												if(data.code === "0000"){
														if(ZONE_ID === data.datas.zoneId){
															window.location = "${pageContext.request.contextPath}/summary/details?zoneId="+ZONE_ID;
														}else{
								            				$("#alertMsg").text(msg);
															$("#alertDlg").dialog({
																height: 200,
																width: 400,
																modal: true,
																buttons: {
																	Ok: function() {
																		window.location.reload(true);
																		$(this).dialog("close");
																	}
																}
															});
														}
													
												}else{
												}
											}else{
							            		$.alert(data.code, data.msg);
											}
							            }
							        });
						        }else{
						        	window.location = "${pageContext.request.contextPath}/summary/details?zoneId="+ZONE_ID;
						        }
							}
						}
					}else{
	            		$.alert(data.code, data.msg);
					}
	            }
	        });
		};
		
		$("#btnLogin").click(function(){
			if(loggedinByPidPage){
				$.Login(false, true, false);
			}else if(loggedinByREPPage){
				$.Login(false, false, true);
			}else{
				$.Login(true, false, false);
			}
		});
		
		$("#btnCancel").click(function(){
			$("#txtUsername").val("");
			$("#txtPassword").val("");
			$("#dlgLogin").dialog("close");
		});
		
	});
</script>

<div class="header">
	<img src="${pageContext.request.contextPath}/images/logo_th.png" height="70px" class="header-icon"/>
	<div class="header-name">ระบบติดตามการเบิกจ่ายเงินกองทุน (e-Financial Tracking)</div><br>
	<div class="header-sub-name">สำนักงานหลักประกันสุขภาพแห่งชาติ</div>
	<div class="user">
		<div style="margin-top:auto;">${USER.titlename}${USER.fullname}</div><br>
		<div style="margin-top:auto;margin-bottom:5px;">${USER.officeName}</div><br>
		<div style="margin-top:auto"><span id="btnLogout" class="pointer-cursor"><img src="${pageContext.request.contextPath}/images/signout.png" width="20px" style="vertical-align: text-top;"/> <strong>ออกจากระบบ</strong></span></div>
	</div>
</div>
<div style="height: 30px; margin: 0; position: relative;">
	<ul class="nav">
		<!-- <li><a href='#'><span>Fund Management</span></a></li>-->
		<li id="summaryTrackStation"><a href='${pageContext.request.contextPath}'><span>สรุปสถานะการเบิกจ่าย</span></a></li>
		<li id="summarypid" style="cursor: pointer;"><strong>ค้นหาข้อมูลรายบุคคล</strong></li>
		<li id="summaryrep" style="cursor: pointer;"><strong>ค้นหาตาม REP ทั้งหมด</strong></li>
		<!-- <li><a href='#'><span>Track Spending</span></a></li>
		<li><a href='#'><span>Forecast your spending</span></a></li>
		<li><a href='#'><span>Track Appeal</span></a></li>
		<li><a href='#'><span>Track Deny</span></a></li>-->
		<li style="border: none;">
			<span class="version">version 1.9.2.16082015</span>
		</li>
	</ul>
</div>

<div class="loader"></div>


<div id="logoutDlg" style="display:none;">
	<form action="">
		<div class="fg-black" style="text-align: center;">
			<div style="margin: 30px; margin-top: 10px;">ต้องการออกจากระบบ?</div>
			<!-- <div>
				<button id="btnLogout" name="btnLogout">Logout</button>
				<button id="btnCancel">Cancel</button>
			</div>-->
		</div>
	</form>
</div>	
<div id="alertDlg" style="display:none;">
	<div class="fg-black" style="text-align: center;">
		<div style="margin: 30px; margin-top: 10px;" id="alertMsg"></div>
	</div>
</div>
<div id="dlgLogin" style="display:none; border:none;">
		<div class="form-login">
			<div>
				<img src="${pageContext.request.contextPath}/images/logo_small.png"/>
			</div>
			<div>
				<span style="margin-right: 12px;">ประเภทผู้ใช้:</span>
				<input type="radio" name="userType" value="E" style="width:auto; vertical-align: middle;"/>&nbsp;สปสช.&nbsp;
				<input type="radio" name="userType" value="H" style="width:auto; vertical-align: middle;" checked/>&nbsp;หน่วยบริการ
			</div>
			<div>
				<span style="margin-right: 20px;">โดเมน:</span>
				<select id="domainName" name="domainName" style="width: 150px;">
				</select>
			</div>
			<div>
				<span style="margin-right: 20px;">ชื่อผู้ใช้:</span>&nbsp;
				<input type="text" name="txtUsername" id="txtUsername" placeholder="Username">
			</div>
			<div>
				<span style="margin-right: 15px;">รหัสผ่าน:</span>&nbsp;
				<input type="password" name="txtPassword" id="txtPassword" placeholder="Password">
			</div>
			<div>
				<button id="btnLogin">Login</button>
				<button id="btnCancel">Cancel</button>
			</div>
		</div>
	</div>